package wcg;


import java.awt.*;
import java.io.*;

import javax.swing.JTextArea;

public class OutputTextArea extends TextArea {
  private TextAreaOutputStream out;
  
  private static final long serialVersionUID = 1L;
  
  public OutputTextArea() throws HeadlessException {
    super();
    this.setEditable(false);
    out = new TextAreaOutputStream(this);
  }
  
  public void setToSystemOut(){
    System.setOut(new PrintStream(this.getOut()));
  }

  public void setToSystemErr(){
    System.setErr(new PrintStream(this.getOut()));
  }

  public TextAreaOutputStream getOut() {
    return out;
  }

  public void flush(){
    this.append(out.toString());
    out.reset();
  }
  public void resetContent() {
	  this.selectAll();
	  this.replaceRange("", 0, this.getSelectionEnd());
  }
  
}

class TextAreaOutputStream extends ByteArrayOutputStream {
  private OutputTextArea textarea;
  
  public TextAreaOutputStream(OutputTextArea textarea) {
    super();
    this.textarea = textarea;
  }

  public synchronized void write(byte[] b, int off, int len) {
    super.write(b, off, len);
    textarea.flush();
  }

  public synchronized void write(int b) {
    super.write(b);
    textarea.flush();
  }

  public void write(byte[] b) throws IOException {
    super.write(b);
   textarea.flush();
  }
  
}